package com.example.demo1.controller;
/**
 * ClassName : com.example.demo1.controller.HelloController
 * Desc : TODO
 * @date : 2022/5/15 15:28
 * @author : Linyh
 */

import com.example.demo1.mapper.LoginMapper;
import com.example.demo1.mapper.UserMapper;
import com.example.demo1.pojo.Login;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


@Controller
public class LoginController {

    @Autowired
    private LoginMapper loginMapper;

    @RequestMapping({"/","/index.html"})
    public String index(){
        return "login";
    }

    @PostMapping("/user/login")
    public String loginUser(@RequestParam("username") String username,
                            @RequestParam("passwd") String passwd,
                            HttpServletRequest request,
                            Model model){
        Login login = loginMapper.loginUser(username,passwd);
        if(login!=null){
            request.getSession().setAttribute("session_user",login);
            return "redirect:/user/userList";
        }else {
            model.addAttribute("msg","用户账号或密码错误");
            return "login";
        }
    }

}
